﻿
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Guids;
using Volo.Abp.Identity;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Uow;


namespace Tool.Data
{
    public class ToolDataSeederContributor : IDataSeedContributor, ITransientDependency
    {
        //private readonly IdentityRoleManager _roleManager;
        //private readonly IdentityUserManager _userManager;
        private readonly IGuidGenerator _guidGenerator;
        private readonly ICurrentTenant _currentTenant;
        private readonly IIdentityUserRepository _userRepository;
        private readonly IIdentityRoleRepository _roleRepository;

        public ToolDataSeederContributor(
            //IdentityRoleManager roleManager,
            //IdentityUserManager userManager,
            IGuidGenerator guidGenerator,
            ICurrentTenant currentTenant,
            IIdentityUserRepository userRepository,
            IIdentityRoleRepository roleRepository
            )
        {
            //_roleManager = roleManager;
            //_userManager = userManager;
            _guidGenerator = guidGenerator;
            _currentTenant = currentTenant;
            this._userRepository = userRepository;
            this._roleRepository = roleRepository;
        }

        public async Task SeedAsync(DataSeedContext context)
        {
            using (_currentTenant.Change(null)) // 主机租户
            {
                // 创建管理员用户
                await CreateUsersAsync();

                // 创建其他角色和用户
                await CreateRolesAsync();
            }
        }


        private async Task CreateRolesAsync()
        {


            if (await _roleRepository.GetCountAsync() <= 0)
            {
                // 管理员角色
                IdentityRole? adminRole = new IdentityRole(_guidGenerator.Create(), "admin");

                await _roleRepository.InsertAsync(adminRole, true);

                // 用户角色
                var userRole = new IdentityRole(_guidGenerator.Create(), "user");
                await _roleRepository.InsertAsync(userRole, true);

                // 操作员角色
                var operatorRole = new IdentityRole(_guidGenerator.Create(), "operator");
                await _roleRepository.InsertAsync(operatorRole, true);
            }
        }

        private async Task CreateUsersAsync()
        {
            // 创建管理员用户
            if (await _userRepository.GetCountAsync() <= 0)
            {
                var adminUser = new IdentityUser(_guidGenerator.Create(), "admin", "admin@myblazorapp.com");

                await _userRepository.InsertAsync(adminUser, true);


                var normalUser = new IdentityUser(_guidGenerator.Create(), "john", "john@myblazorapp.com");

                await _userRepository.InsertAsync(normalUser, true);


                var operatorUser = new IdentityUser(_guidGenerator.Create(), "operator", "operator@myblazorapp.com");

                await _userRepository.InsertAsync(operatorUser, true);

                var testUser = new IdentityUser(_guidGenerator.Create(), "testuser", "test@example.com");

                await _userRepository.InsertAsync(testUser, true);
            }



        }
    }
}
